//
//  YWSelectPersonTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/30.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectPersonTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWHandler.h"

#import "YWMaterialStock.h"
#import "YWMaterial.h"

@interface YWSelectPersonTableViewCell(){
    BOOL isSelectPerson;
}
@property (nonatomic, strong) UIButton *selectButton;
@end

@implementation YWSelectPersonTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (UIButton *)newSelectButton{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"+" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    button.layer.borderColor = [UIColor customGreenColor].CGColor;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 5.0;
    [button addTarget:self action:@selector(handleSelectPersonButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (UILabel *)newPersonNameLabel{
    UILabel * label = [[UILabel alloc] init];
    label.font = kContentFont;
    label.backgroundColor = [UIColor customLineColor];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.lineBreakMode = NSLineBreakByTruncatingMiddle;
    return label;
}

- (UIButton *)newCancelButton{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"trouble_no_select"] forState:UIControlStateNormal];
    button.layer.borderColor = [UIColor customRedColor].CGColor;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 10.0;
   
    [button addTarget:self action:@selector(handleCancelSelectButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    button.contentEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
    return button;
}

- (void)setUpSubviews{
    _selectButton = [self newSelectButton];
    _selectButton.frame = CGRectMake(10, 5, KNameLabelWidth, KNameLabelHeight);
    [self.contentView addSubview:_selectButton];
}

-(void)setBtnTag:(NSInteger)btnTag{
    _btnTag = btnTag;
    _selectButton.tag = btnTag;
}

- (void)setMaterialsArray:(NSMutableArray *)materialsArray{
    _materialsArray = materialsArray;
    isSelectPerson = NO;
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_materialsArray enumerateObjectsUsingBlock:^(YWMaterialStock*  _Nonnull viewModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UILabel * label = [self newPersonNameLabel];
        label.frame = CGRectMake(10 + KNameLabelWidth * (idx % 3) + KLabelGap * (idx % 3),10 + (idx/3)*(KNameLabelHeight+KcontentGap), KNameLabelWidth, KNameLabelHeight);
        label.text = [NSString stringWithFormat:@"%@*%ld",viewModel.materialInfo.materialName?:@"",(long)viewModel.selectQuantity];
        [self.contentView addSubview:label];
        
        UIButton *cancelButton = [self newCancelButton];
        cancelButton.tag = 1000 + idx;
        cancelButton.frame = CGRectMake(CGRectGetMaxX(label.frame)-8, 3 + (idx/3)*(KNameLabelHeight+KcontentGap), KCancelBtnSize, KCancelBtnSize);
        [self.contentView addSubview:cancelButton];
    }];
    self.selectButton.frame = CGRectMake(10 + KNameLabelWidth * (_materialsArray.count % 3) + KLabelGap * (_materialsArray.count % 3), (_materialsArray.count/3)*(KNameLabelHeight+KcontentGap) + 10, KNameLabelWidth, KNameLabelHeight);
    [self.contentView addSubview:self.selectButton];
    
}

- (void)setSelectedContactsArray:(NSMutableArray *)selectedContactsArray{
    _selectedContactsArray = selectedContactsArray;
    isSelectPerson = YES;
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_selectedContactsArray enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UILabel * label = [self newPersonNameLabel];
        label.frame = CGRectMake(10 + KNameLabelWidth * (idx % 3) + KLabelGap * (idx % 3),10 + (idx/3)*(KNameLabelHeight+KcontentGap), KNameLabelWidth, KNameLabelHeight);
        label.text = obj.userName;
        [self.contentView addSubview:label];
        
        UIButton *cancelButton = [self newCancelButton];
        cancelButton.tag = 1000 + idx;
        cancelButton.frame = CGRectMake(CGRectGetMaxX(label.frame)-8, 3 + (idx/3)*(KNameLabelHeight+KcontentGap), KCancelBtnSize, KCancelBtnSize);
        [self.contentView addSubview:cancelButton];
    }];
    self.selectButton.frame = CGRectMake(10 + KNameLabelWidth * (_selectedContactsArray.count % 3) + KLabelGap * (_selectedContactsArray.count % 3), (_selectedContactsArray.count/3)*(KNameLabelHeight+KcontentGap) + 10, KNameLabelWidth, KNameLabelHeight);
    [self.contentView addSubview:self.selectButton];
}

- (void)handleSelectPersonButtonEvent:(UIButton*)sender{
    NSInteger tag = sender.tag;
    if (tag == KReceiveAddBtnTag) {
        if ([self.delegate respondsToSelector:@selector(jumpToselectPerson:)]) {
            [self.delegate jumpToselectPerson:tag];
        }
    }else if (tag == KSendAddBtnTag){
        if ([self.delegate respondsToSelector:@selector(jumpToselectPerson:)]) {
            [self.delegate jumpToselectPerson:tag];
        }
    }else{//物料
        if ([self.delegate respondsToSelector:@selector(jumpToselectPerson:)]) {
            [self.delegate jumpToselectPerson:tag];
        }
    }
}

- (void)handleCancelSelectButtonEvent:(UIButton*)sender{
    NSInteger tag = sender.tag-1000;
    if (isSelectPerson) {
        [self.selectedContactsArray removeObjectAtIndex:tag];
    }else{
        [self.materialsArray removeObjectAtIndex:tag];
    }
    
    if ([self.delegate respondsToSelector:@selector(cancelButtonEvent:)]) {
        [self.delegate cancelButtonEvent:tag];
    }
}

@end
